import ctypes

class SPoint(ctypes.Structure):
    _fields_ = [
        ("x", ctypes.c_int),
        ("y", ctypes.c_int)
    ]

class HelloWorldClass:
    
    def __init__(self) -> None:
        ctypes.cdll.LoadLibrary('lib\\libwinpthread-1.dll')
        ctypes.cdll.LoadLibrary('lib\\libgcc_s_seh-1.dll')
        ctypes.cdll.LoadLibrary('lib\\libstdc++-6.dll')
        self.dll = ctypes.cdll.LoadLibrary('lib\\libcmake_dll_helloworld.dll')
        
        self.dll.create_HelloWorldClass.argtypes = [ctypes.c_int] # 参数类型 # python格式转c格式
        self.dll.create_HelloWorldClass.restype = ctypes.c_void_p # 返回类型 # c 转python

        self.dll.HelloWorldClass_say_hello.argtypes = [ctypes.c_void_p]

        self.dll.HelloWorldClass_set_mnvar1.argtypes=[ctypes.c_void_p, ctypes.c_int]

        self.dll.HelloWorldClass_get_mnvar1.argtypes=[ctypes.c_void_p]
        self.dll.HelloWorldClass_get_mnvar1.restype=ctypes.c_int

        self.dll.HelloWorldClass_get_mfvar2.argtypes=[ctypes.c_void_p] # float成员函数
        self.dll.HelloWorldClass_get_mfvar2.restype=ctypes.c_float

        
        self.dll.HelloWorldClass_get_msvar3.argtypes=[ctypes.c_void_p] # string成员函数 
        self.dll.HelloWorldClass_get_msvar3.restype=ctypes.c_char_p
        
        self.dll.HelloWorldClass_set_msvar3.argtypes = [ctypes.c_void_p, ctypes.c_char_p] # set_msvar3
        
        self.dll.HelloWorldClass_set_mnvar5s.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), ctypes.c_size_t] # set_mnvar5s
        
        self.dll.HelloWorldClass_get_mnvar5s.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]
        
        
        self.pobj = self.dll.create_HelloWorldClass(1220) # 创建对象
        
    def __del__(self):
        # print('del')
        self.dll.HelloWorldClass_destory.argtypes = [ctypes.c_void_p]
        self.dll.HelloWorldClass_destory(self.pobj)
        
    
    def say_hello(self):
        self.dll.HelloWorldClass_say_hello(self.pobj)
        
    def get_mnvar1(self):
        return self.dll.HelloWorldClass_get_mnvar1(self.pobj)
    
    # string_cpp to *char_c to str_python
    def get_msvar3(self):  
        svar3 = self.dll.HelloWorldClass_get_msvar3(self.pobj) # c 接口类型
        svar3 = svar3.decode('utf-8', errors='ignore') # 转换
        return svar3
        
    def set_msvar3(self, svar3):
        self.dll.HelloWorldClass_set_msvar3(self.pobj, svar3) # c 接口类型
        
    # list_python 转 *int_c 转 vector_cpp
    def set_mnvar5s(self, var=[1,2]):
        parr = (ctypes.c_int * len(var)) (*var) # py转为c类型
        self.dll.HelloWorldClass_set_mnvar5s(self.pobj, parr, len(var))
    # vector_cpp 转 *int_c 转 list_python
    def get_mnvar5s(self, le=2):
        pvar = (ctypes.c_int * le)() # c格式
        self.dll.HelloWorldClass_get_mnvar5s(self.pobj, pvar, le)
        var_py = list(pvar) # c 转py
        return var_py
    
    
    # int[5]
    def get_mnvar4s(self):
        self.dll.HelloWorldClass_get_mnvar4s.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]
        # self.dll.HelloWorldClass_get_mnvar4s.restype = ctypes.POINTER(ctypes.c_int)
        pvar = (ctypes.c_int * 5)() # c格式
        # pvar = self.dll.HelloWorldClass_get_mnvar4s(self.pobj)
        self.dll.HelloWorldClass_get_mnvar4s(self.pobj, pvar)
        var_py = list(pvar)
        return var_py
       
    def set_mnvar4s(self, var=[1,2,3,4,5]):
        self.dll.HelloWorldClass_set_mnvar4s.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int)]
        pvar = (ctypes.c_int * 5)(*var) # list 转 c格式(ctypes相关)
        self.dll.HelloWorldClass_set_mnvar4s(self.pobj,pvar)
        
    def get_mspoint(self):
        self.dll.HelloWorldClass_get_mspoint.argtypes = [ctypes.c_void_p]
        self.dll.HelloWorldClass_get_mspoint.restype = SPoint
        spoint_ctypes = SPoint()
        spoint_ctypes = self.dll.HelloWorldClass_get_mspoint(self.pobj)
        return spoint_ctypes
    
    def set_mspoint(self,spoint_ctypes):
        self.dll.HelloWorldClass_set_mspoint.argtypes = [ctypes.c_void_p,SPoint]
        
        self.dll.HelloWorldClass_set_mspoint(self.pobj, spoint_ctypes)
    
    
    
if __name__ == '__main__':
    print('xx')
    obj = HelloWorldClass()
    # obj.say_hello()
    # print(obj.get_mnvar1())
    
    # print(obj.get_msvar3())
    
    # obj.set_msvar3(b'pyth111111') # 长度<15? string
    # print(obj.get_msvar3()+'dd')
    # # print(b'python_bytes_value')
    
    # obj.set_mnvar5s([3124341,33333,666]) # vector<int>
    # print(obj.get_mnvar5s(le=3))
    
    obj.set_mnvar4s(var=[1,2,3,99999,9])
    print(obj.get_mnvar4s())

    obj.set_mnvar4s(var=[1,288,3,999,9])
    print(obj.get_mnvar4s())
    
    # sp = obj.get_mspoint()
    # print(sp.x)
    # print(sp.y)
    spoint_ctypes = SPoint(10,101)
    obj.set_mspoint(spoint_ctypes)
    sp = obj.get_mspoint()
    print(sp.x)
    print(sp.y)